package org.jod.anarchytrek.util;

import java.util.Random;

public class MathUtil {

    public static double round(double value, int decimalPlace) {
        double power_of_ten = 1;
        double fudge_factor = 0.05;
        while (decimalPlace-- > 0) {
            power_of_ten *= 10.0d;
            fudge_factor /= 10.0d;
        }
        return Math.round((value + fudge_factor) * power_of_ten) / power_of_ten;
    }
    
    public static boolean equals(double aFirstValue, double aSecondValue) {
            return round(aFirstValue, 2) == round(aSecondValue, 2);
    }
    
    public static boolean major(double aFirstValue, double aSecondValue) {
        return round(aFirstValue, 2) > round(aSecondValue, 2);
    }    
    
    public static boolean minor(double aFirstValue, double aSecondValue) {
        return round(aFirstValue, 2) < round(aSecondValue, 2);
    }
    
    public static int randombetween(int aMin, int aMax) {
        int min = Math.min(aMin, aMax);
        int delta = Math.abs(aMin - aMax);
        Random random = new Random();
        return random.nextInt(delta) + min;
    }
    
    public static int integerPart(double aValue) {
        return ((int)aValue); 
    }
    
    public static double decimalPart(double aValue) {
        return  round(aValue - ((int)aValue),2); 
    }    
}
